package psd.model.Prolog.Converters;

import java.util.List;

import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ConstructorInvocation;
import org.eclipse.jdt.core.dom.SuperConstructorInvocation;

import psd.model.IConverter;
import psd.model.Prolog.FactGenerationToolbox;
import psd.model.Prolog.PrologConverter;


public class PrologBlockConverter extends PrologConverter implements
		IConverter<Block> {

	public PrologBlockConverter(FactGenerationToolbox toolbox) {
		super(toolbox);		
	}

	@Override
	public boolean convert(Block node) {
		  if(node.getParent().getNodeType() == 31 && node.statements().size() > 0 && (node.statements().get(0) instanceof SuperConstructorInvocation))
	        {
	            List statements = node.statements();
	            SuperConstructorInvocation sci = (SuperConstructorInvocation)statements.get(0);
	            String constructorID = generationToolBox.getIDResolver().getID(sci);
	            String execID = generationToolBox.getIDResolver().getID();
	            statements.remove(0);
	            String childrenlist = generationToolBox.getIDResolver().getIDs(PrologCommon.expandList(node.statements().iterator()));
	            if(!childrenlist.equals("[]"))
	            {
	                childrenlist = (new StringBuilder("[")).append(execID).append(", ").append(childrenlist.substring(1)).toString();
	            } else
	            {
	                childrenlist = (new StringBuilder("[")).append(execID).append("]").toString();
	            }
	            statements.add(0, sci);
	            String execArgs[] = {
	                execID, generationToolBox.getIDResolver().getID(node), generationToolBox.getIDResolver().getID(getEnclosingNode(node)), constructorID, this.getFilePath(node), this.getLineNumber(node)
	            };
	            String applyArgs[] = {
	                constructorID, execID, idResolver.getID(getEnclosingNode(node)), idResolver.getID(sci.getExpression()), "super", idResolver.getIDs(sci.arguments()), idResolver.getID(sci.resolveConstructorBinding()), this.getFilePath(node), this.getLineNumber(node)
	            };
	            String args[] = {
	                childrenlist, this.getFilePath(node), this.getLineNumber(node)
	            };
	            createBodyFact(node, "blockT", args);
	            //generationToolBox.getFactWriter().addIndention();
	            writer.writeFact("execT", execArgs);
	            writer.writeFact("slT", new String[] {
	                execID, Integer.toString(sci.getStartPosition()), Integer.toString(sci.getLength()), this.getFilePath(node), this.getLineNumber(node)
	            });
	            writer.writeFact("callT", applyArgs);
	            writer.writeFact("slT", new String[] {
	            		idResolver.getID(sci), Integer.toString(sci.getStartPosition()), Integer.toString(sci.getLength() - 1), this.getFilePath(node), this.getLineNumber(node)
	            });
	            writeSourceLocationArgumentRaw(sci, "identifier(super)", node.getStartPosition(), "super".length());
	        } else
	        if(node.getParent().getNodeType() == 31 && node.statements().size() > 0 && (node.statements().get(0) instanceof ConstructorInvocation))
	        {
	            List statements = node.statements();
	            ConstructorInvocation sci = (ConstructorInvocation)statements.get(0);
	            String constructorID = idResolver.getID(sci);
	            String execID = idResolver.getID();
	            statements.remove(0);
	            String childrenlist = idResolver.getIDs(PrologCommon.expandList(node.statements().iterator()));
	            if(!childrenlist.equals("[]"))
	            {
	                childrenlist = (new StringBuilder("[")).append(execID).append(", ").append(childrenlist.substring(1)).toString();
	            } else
	            {
	                childrenlist = (new StringBuilder("[")).append(execID).append("]").toString();
	            }
	            statements.add(0, sci);
	            String execArgs[] = {
	                execID, idResolver.getID(node), idResolver.getID(getEnclosingNode(node)), constructorID
	            };
	            String applyArgs[] = {
	                constructorID, execID, idResolver.getID(getEnclosingNode(node)), PrologCommon.quote("null"), PrologCommon.quote("this"), idResolver.getIDs(sci.arguments()), idResolver.getID(sci.resolveConstructorBinding())
	            };
	            String args[] = {
	                childrenlist ,this.getFilePath(node), this.getLineNumber(node)
	            };
	            createBodyFact(node, "blockT", args);
	            //writer.addIndention();
	            writer.writeFact("execT", execArgs);
	            writer.writeFact("slT", new String[] {
	                execID, Integer.toString(sci.getStartPosition()), Integer.toString(sci.getLength()), this.getFilePath(node), this.getLineNumber(node)
	            });
	            writer.writeFact("callT", applyArgs);
	            writer.writeFact("slT", new String[] {
	                idResolver.getID(sci), Integer.toString(sci.getStartPosition()), Integer.toString(sci.getLength() - 1), this.getFilePath(node), this.getLineNumber(node)
	            });
	            writeSourceLocationArgumentRaw(sci, "identifier(this)", node.getStartPosition(), "this".length());
	        } else
	        {
	            String childrenlist = idResolver.getIDs(PrologCommon.expandList(node.statements().iterator()));
	            String args[] = {
	                childrenlist,this.getFilePath(node), this.getLineNumber(node)
	            };
	            createBodyFact(node, "blockT", args);
	            //writer.addIndention();
	        }
	        return true;
	}


}
